package com.tencent.hummingbird.portal.service.impl;

import com.tencent.hummingbird.portal.pojo.Tag;
import com.tencent.hummingbird.portal.mapper.TagMapper;
import com.tencent.hummingbird.portal.service.ITagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxb
 * @since 2024-01-10
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {
    private List<Tag> tags = new CopyOnWriteArrayList<>();
    private Map<String,Tag> nameToTagMap =new ConcurrentHashMap<>();
    @Override
    public List<Tag> getTags() {
        if(tags.isEmpty()){
            synchronized (tags){
                if(tags.isEmpty()){
                    this.tags.addAll(list());

                    for (Tag tag : this.tags){
                        nameToTagMap.put(tag.getName(),tag);
                    }
                }
            }
        }
        return tags;
    }

    @Override
    public Map<String, Tag> getNameToTagMap() {
        if(nameToTagMap.isEmpty()){
            getTags();
        }
        return nameToTagMap;

    }
}
